﻿using Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LoginPanel : BasePanel
{
    private Button loginBtn;
    private Button regBtn;
    private Button closeBtn;
    private InputField nameInput;
    private InputField pwdInput;
    private LoginRequest loginRequest;

    private void Start()
    {
        loginBtn = this.transform.Find("LoginBtn").GetComponent<Button>();
        loginBtn.onClick.AddListener(OnLogin);
        regBtn = this.transform.Find("RegisteBtn").GetComponent<Button>();
        regBtn.onClick.AddListener(OnReg);
        closeBtn = transform.Find("CloseBtn").GetComponent<Button>();
        closeBtn.onClick.AddListener(OnClose);

        nameInput = this.transform.Find("UserNameInput").GetComponent<InputField>();
        pwdInput = transform.Find("PwdInput").GetComponent<InputField>();
        loginRequest = GetComponent<LoginRequest>();
    }

    public override void OnEnter()
    {
        base.OnEnter();
        //gameObject.SetActive(true);
    }

    private void OnLogin()
    {
        PlayClickSound();
        Debug.Log("登陆");
        string msg = string.Empty;
        if (string.IsNullOrEmpty(nameInput.text))
            msg += "用户名不能为空";
        else if (string.IsNullOrEmpty(pwdInput.text))
            msg += "密码不能为空";
        if (!string.IsNullOrEmpty(msg))
        {
            uiMgr.ShowMessage(msg);
            return;
        }
        loginRequest.SendRequest(nameInput.text, pwdInput.text);
    }

    public void OnLoginResponse(ReturnCode code)
    {
        switch (code)
        {
            case ReturnCode.Success:
                uiMgr.ShowMessageSync("登陆成功");
                uiMgr.PushPanelSync(UIPanelType.Room);
                break;
            case ReturnCode.Fail:
                //登陆失败
                uiMgr.ShowMessageSync("用户名或密码错误，无法登录，请重新输入！！");
                break;
        }
    }

    private void OnReg()
    {
        PlayClickSound();
        uiMgr.PushPanel(UIPanelType.Register);
    }

    private void OnClose()
    {
        uiMgr.PopPanel();
    }

    public override void OnPause()
    {
        base.OnPause();
        //gameObject.SetActive(false);
    }

    public override void OnResume()
    {
        base.OnResume();
        //gameObject.SetActive(true);
    }

    public override void OnExit()
    {
        base.OnExit();
        //gameObject.SetActive(false);
    }
}
